package com.eleadmin.store.biz.impl;

import com.eleadmin.damon.utils.constant.SqlConstant;
import com.eleadmin.damon.utils.constant.UserConstant;
import com.eleadmin.damon.utils.results.BaseDao;
import com.eleadmin.damon.utils.results.PageResult2;

import com.eleadmin.damon.utils.utils.Utils;
import com.eleadmin.common.system.entity.User;
import com.eleadmin.store.biz.WarehouseBiz;
import com.eleadmin.store.entity.Warehouse;
import com.eleadmin.common.core.web.JsonResult;
import com.eleadmin.store.result.SelectResult;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 * 数据层实现类
 *
 * @author : Doman
 * @version ：1.0
 * @date : 2024-02-12 14:23:33
 */
@Repository
public class WarehouseBizImpl extends BaseDao implements WarehouseBiz {

    @Override
    public JsonResult list(PageResult2<T> page, Warehouse warehouse, User loginUser) throws Exception {
        Map<String, String> map = new HashMap<String, String>();
        RowMapper<Warehouse> rm = new BeanPropertyRowMapper<Warehouse>(Warehouse.class);
        StringBuffer sql = new StringBuffer("select w.id,w.name,w.organization_id");
        sql.append(",w.code,w.orderNum,w.remark,w.create_time");
        sql.append(",w.create_user,w.update_time,w.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nickname createName,suser2.nickname updateName ");
        sql.append(" from we_warehouse w ");
        sql.append(" inner join sys_user suser on suser.user_id=w.create_user ");
        sql.append(" left join sys_user suser2 on suser2.user_id=w.update_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=w.organization_id and sogzn.deleted=0 ");
        sql.append(" where w.deleted=0 ");
        if (loginUser.getUserId() != UserConstant.ADMIN_ID) {
            sql.append(" and w.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
        if (Utils.isNotEmpty(warehouse)) {
            if (Utils.isNotEmpty(warehouse.getName())) {
                sql.append(" and (instr(w.name,:name)>0 or instr(w.code,:name)>0) ");
                map.put("name", warehouse.getName());
            }
            if (Utils.isNotEmpty(warehouse.getSort()) && Utils.isNotEmpty(warehouse.getOrder())) {
                sql.append(" order by w." + warehouse.getSort() + " " + warehouse.getOrder());
            } else {
                sql.append(" order by w.create_time desc");
            }
        }
        return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
    }

    @Override
    public List<Warehouse> list(Warehouse warehouse, User loginUser) {
        Map<String, String> map = new HashMap<String, String>();
        RowMapper<Warehouse> rm = new BeanPropertyRowMapper<Warehouse>(Warehouse.class);
        StringBuffer sql = new StringBuffer("select w.id,w.name,w.organization_id");
        sql.append(",w.code,w.orderNum,w.remark,w.create_time");
        sql.append(",w.create_user,w.update_time,w.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nickname createName ");
        sql.append(" from we_warehouse w ");
        sql.append(" inner join sys_user suser on suser.user_id=w.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=w.organization_id and sogzn.deleted=0 ");
        sql.append(" where w.deleted=0 ");
        if (loginUser.getUserId() != UserConstant.ADMIN_ID) {
            sql.append(" and w.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
        if (Utils.isNotEmpty(warehouse)) {
            if (Utils.isNotEmpty(warehouse.getName())) {
                sql.append(" and (instr(w.name,:name)>0 or instr(w.code,:name)>0) ");
                map.put("name", warehouse.getName());
            }
            if (Utils.isNotEmpty(warehouse.getSort()) && Utils.isNotEmpty(warehouse.getOrder())) {
                sql.append(" order by w." + warehouse.getSort() + " " + warehouse.getOrder());
            } else {
                sql.append(" order by w.create_time desc");
            }
        }
        getMapParameter(sql, map);
        return jdbcTemplate.query(sql.toString(), map, rm);
    }

    @Override
    public List<SelectResult> listSel(Warehouse warehouse, User loginUser) {
        Map<String, String> map = new HashMap<String, String>();
        RowMapper<SelectResult> rm = new BeanPropertyRowMapper<SelectResult>(SelectResult.class);
        StringBuffer sql = new StringBuffer("select w.id value,w.name label");
        sql.append(" from we_warehouse w ");
        sql.append(" where w.deleted=0 ");
        if (loginUser.getUserId() != UserConstant.ADMIN_ID) {
            sql.append(" and w.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
        if (Utils.isNotEmpty(warehouse)) {
            if (Utils.isNotEmpty(warehouse.getName())) {
                sql.append(" and (instr(w.name,:name)>0 or instr(w.code,:name)>0) ");
                map.put("name", warehouse.getName());
            }
            if (Utils.isNotEmpty(warehouse.getSort()) && Utils.isNotEmpty(warehouse.getOrder())) {
                sql.append(" order by w." + warehouse.getSort() + " " + warehouse.getOrder());
            } else {
                sql.append(" order by w.create_time desc");
            }
        }
        sql.append(" limit "+ SqlConstant.LIMIT);

        getMapParameter(sql, map);
        return jdbcTemplate.query(sql.toString(), map, rm);
    }

    @Override
    public Warehouse getById(String id) {
        RowMapper<Warehouse> rm = new BeanPropertyRowMapper<Warehouse>(Warehouse.class);
        Map<String, String> map = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("select w.id,w.name,w.organization_id");
        sql.append(",w.code,w.orderNum,w.remark,w.create_time");
        sql.append(",w.create_user,w.update_time,w.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nickname createName ");
        sql.append(" from we_warehouse w ");
        sql.append(" inner join sys_user suser on suser.user_id=w.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=w.organization_id and sogzn.deleted=0 ");
        sql.append(" where w.deleted=0 and w.id=:id ");
        map.put("id", id);
        getMapParameter(sql, map);
        try {
            return jdbcTemplate.queryForObject(sql.toString(), map, rm);
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public boolean save(Warehouse warehouse, User loginUser) {
        Map<String, String> map = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("insert into we_warehouse (");
        sql.append("name,organization_id,code,orderNum");
        sql.append(",remark,create_user,create_time) values ( ");
        sql.append(":name,:organization_id,:code,:orderNum");
        sql.append(",:remark,:create_user,now())");
        map.put("name", warehouse.getName());
        map.put("organization_id", loginUser.getOrganizationId() + "");
        map.put("code", warehouse.getCode());
        map.put("orderNum", warehouse.getOrdernum() == null ? null : warehouse.getOrdernum() + "");
        map.put("remark", warehouse.getRemark());
        map.put("create_user", loginUser.getUserId() + "");
        getMapParameter(sql, map);
        int count = jdbcTemplate.update(sql.toString(), map);
        return count > 0;
    }

    @Override
    public boolean update(Warehouse warehouse, User loginUser) {
        Map<String, String> map = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("update we_warehouse set ");
        sql.append("name=:name,code=:code,orderNum=:orderNum ");
        sql.append(",remark=:remark,update_time=now(),update_user=:update_user where id=:id ");
        map.put("name", warehouse.getName());
        map.put("code", warehouse.getCode());
        map.put("orderNum", warehouse.getOrdernum() == null ? null : warehouse.getOrdernum() + "");
        map.put("remark", warehouse.getRemark());
        map.put("update_user", loginUser.getUserId() + "");
        map.put("id", warehouse.getId());
        getMapParameter(sql, map);
        int count = jdbcTemplate.update(sql.toString(), map);
        return count > 0;
    }

    @Override
    public boolean remove(String id, User loginUser) {
        int count = 0;
        Map<String, String> map = null;
        if (id.length() > 0) {
            for (String id1 : id.split(",")) {
                map = new HashMap<String, String>();
                if (id1 == null || id1.equals("")) {
                    break;
                }
                StringBuffer sql = new StringBuffer("update we_warehouse set deleted=1" +
                        ",update_user=:update_user, update_time=now() "
                        + " where id =:id ");
                map.put("update_user", loginUser.getUserId() + "");
                map.put("id", id1);
                getMapParameter(sql, map);
                count = jdbcTemplate.update(sql.toString(), map);
            }
        }
        return count > 0;
    }
}
